<template>
	<div class="personal layout-pd">
		<el-row>
			<!-- 个人信息 -->
			<el-col :xs="24" :sm="14">
				<el-card shadow="hover">
					<template #header>
						<span class="personal-edit-title mb15">个人信息</span>
						<span class="personal-info-edit" @click="changetUserMes">修改</span>
					</template>
					<div class="personal-user">
						<div class="personal-user-left">
							<div class="h100 personal-user-left-upload">
								<img :src="state.form.photoPath ? state.form.photoPath : `/src/assets/img/avatar.png`" />
							</div>
						</div>
						<div class="personal-user-right">
							<el-row>
								<el-col :span="24" class="personal-title mb18">{{ currentTime }}，{{ state.form.fullName }}！ </el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">昵称：</div>
											<div class="personal-item-value">{{ state.form.nikeName ? state.form.nikeName : '无' }}</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">身份：</div>
											<div class="personal-item-value">{{ state.form.userTypeName }}</div>
										</el-col>
									</el-row>
								</el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">登录IP：</div>
											<div class="personal-item-value">{{ state.loginIp }}</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">登录时间：</div>
											<div class="personal-item-value">{{ state.loginTime }}</div>
										</el-col>
									</el-row>
								</el-col>
							</el-row>
						</div>
					</div>
				</el-card>
			</el-col>

			<!-- 消息通知 -->
			<el-col :xs="24" :sm="10" class="pl15 personal-info">
				<el-card shadow="hover">
					<template #header>
						<span class="personal-edit-title mb15">消息通知</span>
						<span class="personal-info-more">更多</span>
					</template>
					<div class="personal-info-box">
						<ul class="personal-info-ul">
							<li v-for="(v, k) in state.newsInfoList" :key="k" class="personal-info-li">
								<a :href="v.link" target="_block" class="personal-info-li-title">{{ v.title }}</a>
							</li>
						</ul>
					</div>
				</el-card>
			</el-col>
			<el-col :span="24">
				<el-card shadow="hover" class="mt15 personal-edit">
					<template #header>
						<span class="personal-edit-title mb15">账号安全</span>
            <span v-if="state.needChangeFlag" style="color: red">
              请定期修改密码，绑定手机以提高账户安全性。
            </span>
					</template>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">账户密码</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary" @click="changePassword">立即修改</el-button>
							</div>
						</div>
					</div>

					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定手机</div>
								<div class="personal-edit-safe-item-left-value">{{ getPhone() }}</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary" @click="changeCellphone">立即修改</el-button>
							</div>
						</div>
					</div>

					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定钉钉</div>
								<div class="personal-edit-safe-item-left-value">未开放绑定</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary">立即设置</el-button>
							</div>
						</div>
					</div>
				</el-card>
			</el-col>
		</el-row>
		<zh-dialog style="width: 90%" title="更新信息" v-model="state.visible">
			<UserForm ref="eduPersonalpage" :show-flag="false" :userInfo="state.form" v-model="state.visible"></UserForm>
			<template #footer>
				<span class="dialog-footer">
					<zh-button @click="state.visible = false">取消</zh-button>
					<zh-button v-if="true" type="primary" @click="alertPersonSave"> 保存 </zh-button>
				</span>
			</template>
		</zh-dialog>
		<zh-dialog style="width: 50%" title="修改密码" v-model="state.visiblePasswod">
      <!-- 添加提示信息 -->
      <div v-if="state.needChangePassword" class="password-change-tip">
        您的密码长时间未修改，请及时修改以确保账户安全。
      </div>
			<UserPasswordForm ref="eduPersonPasswordpage" :show-flag="false" :userInfo="state.form" v-model="state.visiblePasswod"></UserPasswordForm>
			<template #footer>
				<span class="dialog-footer">
					<zh-button @click="state.visiblePasswod = false">取消</zh-button>
					<zh-button v-if="true" type="primary" @click="PersonPassWordSave"> 保存 </zh-button>
				</span>
			</template>
		</zh-dialog>
		<zh-dialog style="width: 30%" title="绑定手机" v-model="state.visibleCellphone">
			<PersonCellphoneForm ref="eduPersonCellphonepage" :show-flag="false" :userInfo="state.form" v-model="state.visibleCellphone"></PersonCellphoneForm>
			<template #footer>
				<span class="dialog-footer">
					<zh-button @click="state.visibleCellphone = false">取消</zh-button>
					<zh-button v-if="true" type="primary" @click="PersonCellphoneSave"> 保存 </zh-button>
				</span>
			</template>
		</zh-dialog>
	</div>
</template>

<script lang="ts" setup name="personalPage">
// import validate from '@/utils/validate';
import UserForm from './component/PersonalForm.vue';
import UserPasswordForm from './component/PersonPasswordForm.vue';
import PersonCellphoneForm from './component/PersonCellphoneForm.vue';
const VITE_CHANGEPWD_NAME = import.meta.env.VITE_CHANGEPWD_NAME as string;
import { formatAxis } from '@/utils/formatTime';
import { Session } from '@/utils/storage';
//------------------定义子组件---------------------------
// const userForm = ref<any>({});
// 初始化
onMounted(() => {
	nextTick(() => {
		userInit();
    let changePwd = Session.get(VITE_CHANGEPWD_NAME);
    if (changePwd) {
      state.visiblePasswod = true;
      state.needChangePassword = true;
      state.needChangeFlag=true;
    }
	});
});

const eduPersonalpage = ref();
const eduPersonPasswordpage = ref();
const eduPersonCellphonepage = ref();
//------------------内部变量定义-------------------------
const state = reactive({
	loginTime: $store.user.loginTime ? $store.user.loginTime : Session.get('bifang_loginTime'),
	loginIp: $store.user.loginIp ? $store.user.loginIp : Session.get('bifang_loginIp'),
	statusData: [
		{ dictKey: '1', dictValue: '启用' },
		{ dictKey: '0', dictValue: '禁用' },
	],
	flagData: [
		{ dictKey: '1', dictValue: '是' },
		{ dictKey: '0', dictValue: '否' },
	],

	userTypeDisabled: true,
	dictData: {} as any,
	orgSelectTreeData: [],
	userId:$store.user.userInfo?.userId,
	form: {
		userId: undefined,
		userName: undefined,
		cardType: '01',
		idcard: undefined,
		fullName: undefined,
		sex: undefined,
		birthday: undefined,
		cellphone: undefined,
		officePhone: undefined,
		shortPhone: undefined,
		email: undefined,
		userType: '10',
		status: '1',
		orgIdList: [],
		orgId: undefined,
		orgName: undefined,
		appName: undefined,
		realFlag: '1',
		nikeName: undefined,
		orderNo: undefined,
		userIp: undefined,
		addressDetail: undefined,
		remarks: undefined,
		appId: undefined,
		photoPath: undefined,
		avatarShowId: '',
	},
	modalTitle: '操作',
	visible: false,
	visiblePasswod: false,
	visibleCellphone: false,
  needChangePassword: false,
  needChangeFlag: false,
	newsInfoList: [], //消息列表
});

//------------------定义调用方法-------------------------
// 当前时间提示语
const currentTime = computed(() => {
	return formatAxis(new Date());
});
// 获取手机号码
const getPhone = () => {
	if (!state.form.cellphone) {
		return '未绑定';
	}
	// 手机号仅显示前三位及后四位数字，隐藏信息用*代替
	return '已绑定手机：' + state.form.cellphone.substr(0, 3) + '****' + state.form.cellphone.substring(7);
};

//------------------初始化------------------------------

function userInit() {
	let userParams = {
		userId:state.userId,
	};
	$api.common.send.postCrypto('user0003', userParams).then((res) => {
		let form = Object.assign(state.form, res.result);
		state.form = form;
	});
}

//修改用户基本信息
const changetUserMes = () => {
	state.modalTitle = '修改用户信息';
	state.visible = true;
};

const alertPersonSave = () => {
	eduPersonalpage.value.saveUser().then(() => {
		userInit();
		state.visible = false;
	});
};

//修改用户密码
const changePassword = () => {
	state.visiblePasswod = true;
};
const PersonPassWordSave = async () => {
	await eduPersonPasswordpage.value.savePassword().then(() => {
    //密码修改成功后，清除标识设置为false
    Session.set(VITE_CHANGEPWD_NAME, false);
    state.needChangeFlag=false;
		userInit();
	});
};
// 修改用户手机
const changeCellphone = () => {
	state.visibleCellphone = true;
};
const PersonCellphoneSave = async () => {
	await eduPersonCellphonepage.value.saveCellphone().then(() => {
		userInit();
	});
};
// 数据字典初始化
const params = {} as any;
params.dictFields = 'userType,sex,cardType';
$api.common.getBatchDict.post(params).then((res) => {
	state.dictData = res.result;
});
</script>
<style scoped lang="scss">
@import '@/theme/mixins/index.scss';
.personal {
	.personal-info-edit {
		float: right;
		color: var(--el-text-color-secondary);
		font-size: 13px;
		&:hover {
			color: var(--el-color-primary);
			cursor: pointer;
		}
	}
	.personal-edit-title {
		position: relative;
		padding-left: 10px;
		color: var(--el-text-color-regular);
		&::after {
			content: '';
			width: 2px;
			height: 10px;
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
			background: var(--el-color-primary);
		}
	}
	.personal-user {
		height: 130px;
		display: flex;
		align-items: center;
		.personal-user-left {
			width: 120px;
			height: 120px;
			border-radius: 3px;
			:deep(.el-upload) {
				height: 100%;
			}
			.personal-user-left-upload {
				img {
					width: 100%;
					height: 100%;
					border-radius: 3px;
				}
				&:hover {
					img {
						animation: logoAnimation 0.3s ease-in-out;
					}
				}
			}
		}
		.personal-user-right {
			flex: 1;
			padding: 0 15px;
			.personal-title {
				font-size: 18px;
				@include text-ellipsis(1);
			}
			.personal-item {
				display: flex;
				align-items: center;
				font-size: 13px;
				.personal-item-label {
					color: var(--el-text-color-secondary);
					@include text-ellipsis(1);
				}
				.personal-item-value {
					@include text-ellipsis(1);
				}
			}
		}
	}
	.personal-info {
		.personal-info-more {
			float: right;
			color: var(--el-text-color-secondary);
			font-size: 13px;
			&:hover {
				color: var(--el-color-primary);
				cursor: pointer;
			}
		}
		.personal-info-box {
			height: 130px;
			overflow: hidden;
			.personal-info-ul {
				list-style: none;
				.personal-info-li {
					font-size: 13px;
					padding-bottom: 10px;
					.personal-info-li-title {
						display: inline-block;
						@include text-ellipsis(1);
						color: var(--el-text-color-secondary);
						text-decoration: none;
					}
					& a:hover {
						color: var(--el-color-primary);
						cursor: pointer;
					}
				}
			}
		}
	}
	.personal-recommend-row {
		.personal-recommend-col {
			.personal-recommend {
				position: relative;
				height: 100px;
				border-radius: 3px;
				overflow: hidden;
				cursor: pointer;
				&:hover {
					i {
						right: 0px !important;
						bottom: 0px !important;
						transition: all ease 0.3s;
					}
				}
				i {
					position: absolute;
					right: -10px;
					bottom: -10px;
					font-size: 70px;
					transform: rotate(-30deg);
					transition: all ease 0.3s;
				}
				.personal-recommend-auto {
					padding: 15px;
					position: absolute;
					left: 0;
					top: 5%;
					color: var(--next-color-white);
					.personal-recommend-msg {
						font-size: 12px;
						margin-top: 10px;
					}
				}
			}
		}
	}
	.personal-edit {
		.personal-edit-title {
			position: relative;
			padding-left: 10px;
			color: var(--el-text-color-regular);
			&::after {
				content: '';
				width: 2px;
				height: 10px;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background: var(--el-color-primary);
			}
		}
		.personal-edit-safe-box {
			border-bottom: 1px solid var(--el-border-color-light, #ebeef5);
			padding: 15px 0;
			.personal-edit-safe-item {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.personal-edit-safe-item-left {
					flex: 1;
					overflow: hidden;
					.personal-edit-safe-item-left-label {
						color: var(--el-text-color-regular);
						margin-bottom: 5px;
					}
					.personal-edit-safe-item-left-value {
						color: var(--el-text-color-secondary);
						@include text-ellipsis(1);
						margin-right: 15px;
					}
				}
			}
			&:last-of-type {
				padding-bottom: 0;
				border-bottom: none;
			}
		}
	}
  .password-change-tip {
    color: #f56c6c; // 红色提示
    margin-bottom: 15px;
    padding: 10px;
    background-color: #fef0f0;
    border: 1px solid #fde2e2;
    border-radius: 4px;
  }
}
</style>
